package com.woniuxy.controller;

import com.woniuxy.bean.DataGridView;
import com.woniuxy.bean.ResultDto;
import com.woniuxy.bean.RoleVo;
import com.woniuxy.bean.UserVo;
import com.woniuxy.pojo.Permission;
import com.woniuxy.pojo.Role;
import com.woniuxy.service.PermissionService;
import com.woniuxy.service.RoleService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class RoleController extends BaseController{
    private Logger logger= Logger.getLogger(this.getClass());
    @Autowired
    private RoleService roleService;
    @Autowired
    private PermissionService permissionService;
    @ResponseBody
    @GetMapping(value = "/roles")
    public DataGridView<Role> list(RoleVo roleVo){
        logger.debug("角色名:"+roleVo.getRolename());
        return this.roleService.findRolesByPage(roleVo);
    }
    @RequestMapping(value = "/prepAssignRight")
    public String prepAssignRight(){
        return "sys/assignRight";
    }
    @ResponseBody
    @RequestMapping(value = "/prepPermissions")
    public List<Permission> prepPermissions(){
        return this.permissionService.findAllPermissions();
    }
    @ResponseBody
    @RequestMapping(value = "/getChecked")
    public List<Integer> getChecked(@RequestParam("rid") Integer rid){
        return this.permissionService.selectPerIds(rid);
    }
    @ResponseBody
    @RequestMapping(value = "/assignRight")
    public ResultDto assignRight(@RequestParam("rid") Integer rid,
                                 @RequestParam("perids[]")Integer[] perids){
        ResultDto resultDto=new ResultDto();
        try{
            this.roleService.assignRight(rid,perids);
            resultDto.setCode(1005);
            resultDto.setMsg("分配权限成功");
        }catch (Exception e){
            resultDto.setCode(-1005);
            resultDto.setMsg("分配权限失败");
        }
        return resultDto;

    }
}
